Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.IO
Imports C4F.DevKit.Calendar.Parser
Imports C4F.DevKit.Contacts

Namespace C4F.DevKit.Calendar
	''' <summary>
	''' A class which combines most of the Calendar API functionality into a single class,
	''' so that the features can all be accessed from a single place.
	''' </summary>
	Public Class CalendarManager
		#Region "Properties"

		''' <summary>
		''' The folder where Windows stores its calendar files by default.
		''' </summary>
		Public ReadOnly Property WindowCalendarFolderPath() As String
			Get
				Dim dir As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\Windows Calendar\Calendars")
				If (Not String.IsNullOrEmpty(dir)) AndAlso Directory.Exists(dir) Then
					Return dir
				Else
					Return String.Empty
				End If
			End Get
		End Property

		#End Region ' Properties

		#Region "Public Methods"

		''' <summary>
		''' Creates a new calendar.
		''' </summary>
		''' <param name="filePath">The file in which to save the calendar.</param>
		Public Sub CreateCalendar(ByVal filePath As String)
			Writer.CreateCalendar(New List(Of Appointment)(), filePath)
		End Sub

		''' <summary>
		''' Retrieves all appointments from the specified calendar.
		''' </summary>
		''' <param name="file">The path of the calendar file from which to get
		''' appointments.</param>
		''' <returns>A collection of the appointments from the specified calendar.</returns>
		Public Function GetAllAppointments(ByVal file As String) As IEnumerable(Of Appointment)
			Return Reader.ReadCalendar(file)
		End Function

		''' <summary>
		''' Retrieves all appointments from the specified calendar, optionally looking in
		''' the default Windows Calendar folder path.
		''' </summary>
		''' <param name="useWindowCalendarFolderPath">Whether or not to look for the file in the
		''' folder Windows Calendar stores files in by default</param>
		''' <param name="fileName">If useWindowsCalendarFolderPath is true, this is just the base name
		''' of the file to use. Otherwise, it is the full path of the file.</param>
		''' <returns>A collection of the appointments from the specified calendar.</returns>
		Public Function GetAllAppointments(ByVal useWindowCalendarFolderPath As Boolean, ByVal fileName As String) As IEnumerable(Of Appointment)
			If (Not useWindowCalendarFolderPath) Then
				Return GetAllAppointments(fileName)
			End If

			Dim filePath As String = Path.Combine(WindowCalendarFolderPath, fileName)

			If String.IsNullOrEmpty(filePath) OrElse (Not File.Exists(filePath)) Then
				Throw New FileNotFoundException(String.Format("Could not find the specified calendar file {0}.", fileName))
			End If

			Return GetAllAppointments(filePath)
		End Function

		''' <summary>
		''' Adds appointments to a specified calendar.
		''' </summary>
		''' <param name="filePath">The path of the calendar file to which to add the appointments.</param>
		''' <param name="appts">The appointments to add.</param>
		Public Sub AddAppointments(ByVal filePath As String, ByVal appts As IEnumerable(Of Appointment))
			Writer.AddToCalendar(appts, filePath)
		End Sub

		''' <summary>
		''' Adds an appointment to a specified calendar.
		''' </summary>
		''' <param name="filePath">The path of the calendar file to which to add the appointments.</param>
		''' <param name="appt">The appointment to add.</param>
		Public Sub AddAppointment(ByVal filePath As String, ByVal appt As Appointment)
			appt.Save(filePath)
		End Sub

		''' <summary>
		''' Updates the information in an existing appointment.
		''' </summary>
		''' <param name="appt">The new version of the appointment to put in the calendar. This appointment
		''' will replace the appointment in the Calendar with the same UID.</param>
		''' <param name="filePath">The calendar file to be edited.</param>
		Public Sub EditAppointment(ByVal filePath As String, ByVal appt As Appointment)
			Writer.EditAppointment(appt, filePath)
		End Sub

		''' <summary>
		''' Imports appointments from a calendar into the current calendar.
		''' </summary>
		''' <param name="importCalendar">The path of the calendar to import events from.</param>
		''' <param name="currentCalendar">The path of the file for the current calendar.</param>
		Public Sub ImportCalendar(ByVal importCalendar As String, ByVal currentCalendar As String)
			Writer.AddToCalendar(GetAllAppointments(importCalendar), currentCalendar)
		End Sub

		''' <summary>
		''' Exports appointments from the current calendar to a new file.
		''' </summary>
		''' <param name="exportTarget">The file path of the new calendar to be created by the export.</param>
		''' <param name="currentCalendar">The file path of the current calendar.</param>
		Public Sub ExportCalendar(ByVal exportTarget As String, ByVal currentCalendar As String)
			Writer.CreateCalendar(GetAllAppointments(currentCalendar), exportTarget)
		End Sub

		''' <summary>
		''' Imports an appoointment into a calendar.
		''' </summary>
		''' <param name="currentCalendar">The file path of the calendar to import into.</param>
		''' <param name="appt">The appointment to import.</param>
		Public Sub Import(ByVal currentCalendar As String, ByVal appt As Appointment)
			AddAppointment(currentCalendar, appt)
		End Sub

		''' <summary>
		''' Exports an appointment to a calendar file.
		''' </summary>
		''' <param name="exportTarget">The file path of the calendar to export to.</param>
		''' <param name="appt">The appointment to export.</param>
		Public Sub Export(ByVal exportTarget As String, ByVal appt As Appointment)
			AddAppointment(exportTarget, appt)
		End Sub

		''' <summary>
		''' Gets all windows contacts associated with the user currently logged in.
		''' </summary>
		''' <returns>A list of Attendees converted from a C4F.DevKit.Contacts.SimpleContactView object.</returns>
		''' <remarks>Use the Contacts assembly shipped with the VS Express Developer Kit. </remarks>
		Public Function GetAllWindowsContactsAsAttendees() As List(Of Attendee)
			Dim attendees As List(Of Attendee) = New List(Of Attendee)()
			Dim contactMgr As ContactManager = New ContactManager()

			For Each contact As SimpleContactView In contactMgr.GetSimpleContactCollection()
				If (Not String.IsNullOrEmpty(contact.BusinessEmail)) Then
					attendees.Add(New Attendee(contact.FormattedName,contact.BusinessEmail))
				Else
					attendees.Add(New Attendee(contact.FormattedName,contact.PersonalEmail1))
				End If
			Next contact

			Return attendees
		End Function

		#End Region ' Public Methods
	End Class
End Namespace
